import '../model/tagModel.dart';
import '../model/apiResponse.dart';
import '../constants/apiConstants.dart';
import 'base/apiClient.dart';

class TagService {
  final ApiClient _apiClient = ApiClient();

  // GET /api/v1/bgc/tag/all
  Future<List<TagModel>> getTags() async {
    try {
      String uri = '/api/v1/bgc/tag/all';
      final response =
          await _apiClient.get(uri, headers: ApiConstants.formHeaders);

      final apiResponse = ApiResponse<List<dynamic>>.fromJson(
        response.data,
        (data) => data as List<dynamic>,
      );

      if (apiResponse.isSuccess && apiResponse.data != null) {
        return apiResponse.data!
            .map((json) => TagModel.fromJson(json))
            .where((tag) => tag.isActive) // 只返回激活的标签
            .toList();
      } else {
        throw Exception('获取标签失败: ${apiResponse.message}');
      }
    } catch (e) {
      throw Exception('获取标签失败: $e');
    }
  }
}
